package com.langyiquan.backed.controller;

import com.langyiquan.bo.SysDeptBo;
import com.langyiquan.exception.http.ParamException;
import com.langyiquan.pojo.SysDept;
import com.langyiquan.service.SysDeptService;
import com.langyiquan.utils.UnifyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Api(value = "部门", tags = {"部门的相关接口"})
@RestController
@RequestMapping("dept")
public class SysDeptController extends BaseController {

    @Autowired
    public SysDeptService sysDeptService;

    @ApiOperation(value = "获取部门信息",notes = "获取部门信息",httpMethod = "GET")
    @GetMapping("info")
    public UnifyResponse info(@RequestParam Integer sysDeptId){
        if(sysDeptId == null){
            throw new ParamException(2002);
        }
        SysDept sysDept = sysDeptService.getSysDept(sysDeptId);
        return UnifyResponse.success(sysDept);
    }


    @ApiOperation(value = "保存部门", notes = "保存部门", httpMethod = "POST")
    @PostMapping("/saveDept")
    public UnifyResponse saveDept(@RequestBody SysDeptBo sysDeptBo, HttpServletRequest request) {

        // 设置操作IP
        sysDeptBo.setOperatorIp(getIpAddress(request));
        sysDeptBo.setOperatorId(getPassportUserId(request));

        sysDeptService.saveSysDept(sysDeptBo);

        return UnifyResponse.success();
    }

    @ApiOperation(value = "更新部门", notes = "更新部门", httpMethod = "POST")
    @PostMapping("/updateDept")
    public UnifyResponse updateDept(@RequestBody SysDeptBo sysDeptBo, HttpServletRequest request) {

        sysDeptBo.setOperatorIp(getIpAddress(request));
        sysDeptBo.setOperatorId(getPassportUserId(request));

        sysDeptService.updateSysDept(sysDeptBo);

        return UnifyResponse.success();
    }


    @ApiOperation(value = "删除部门", notes = "删除部门", httpMethod = "POST")
    @PostMapping("/deleteDept")
    public UnifyResponse deleteDept(@RequestParam Integer deptId) {

        sysDeptService.deleteSysDept(deptId);

        return UnifyResponse.success();
    }

}
